﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.Materials;
using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using Microsoft.EntityFrameworkCore;
using T.E.ERP.Write.API.Applicantions.Command.ProcurementsCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.ProcurementsCommandCommand
{
    /// <summary>
    /// 采购发票新增命令处理
    /// </summary>
    public class PurchaseInvoicesHandler : IRequestHandler<AddPurchaseInvoicesCommand, APIResult<object>>
    {
        //采购发票表
        private readonly IBaseRepository<PurchaseInvoice> _purchaseInvoice;
        //物料表
        private readonly IBaseRepository<Material_BasicModel> _material_BasicModel;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 记录异常
        /// </summary>
        private readonly ILogger<PurchaseInvoicesHandler> logger;

        public PurchaseInvoicesHandler(IBaseRepository<PurchaseInvoice> purchaseInvoice, IMapper mapper, IBaseRepository<Material_BasicModel> material_BasicModel, ILogger<PurchaseInvoicesHandler> logger)
        {
            _purchaseInvoice = purchaseInvoice;
            this.mapper = mapper;
            _material_BasicModel = material_BasicModel;
            this.logger = logger;
        }

        public async Task<APIResult<object>> Handle(AddPurchaseInvoicesCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            try
            {
                PurchaseInvoice entity = new PurchaseInvoice();
                entity.DocumentCode = request.DocumentCode;
                entity.DocumentTime = request.DocumentTime;
                entity.DocumentStuct = true;
                entity.BusinessStuct = request.BusinessStuct;
                entity.Documentid = request.Documentid;
                entity.Auditor = request.Auditor;
                entity.Operator = request.Operator;
                entity.AuditDate = request.AuditDate;
                entity.Supid = request.Supid;
                entity.TaxNumber = request.TaxNumber;
                entity.InvoiceType = request.InvoiceType;
                entity.InvoiceCode = request.InvoiceCode;
                entity.InvoiceNumber = request.InvoiceNumber;
                entity.InvoiceDate = request.InvoiceDate;
                entity.Redandblue = request.Redandblue;
                entity.InvoiceAmount = request.InvoiceAmount;
                entity.TaxAmount = request.TaxAmount;
                entity.TotalIncludingTax = request.TotalIncludingTax;
                entity.FinancialStatus = request.FinancialStatus;
                entity.TaxLv = request.TaxLv;
                entity.FinancialStatus = request.FinancialStatus;
                entity.material_code = 12345421234;
                int add1 = _purchaseInvoice.Add(entity).Result;
                if (add1 > 0)
                {
                    request.Materialtab.ForEach(x =>
                    {
                        x.material_code = entity.material_code;
                    });
                    int add2 = _material_BasicModel.AddRanges(request.Materialtab).Result;
                    if (add2 > 0)
                    {
                        result.Code = APIEnum.添加成功;
                        result.Msg = APIEnum.添加成功.ToString();
                    }
                    else
                    {
                        result.Code = APIEnum.添加失败;
                        result.Msg = APIEnum.添加失败.ToString();
                    }
                }
                else
                {
                    result.Code = APIEnum.添加失败;
                    result.Msg = APIEnum.添加失败.ToString();
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "处理请求时发生错误");
                throw;
            }

            return await Task.FromResult(result);
        }
    }
}
